package student.aistar.day13;

import student.aistar.design.singleton.version03.Singleton03;

import java.io.*;

public class ObjectSingletonDemo {
    public static void main(String[] args) throws Exception {
        Singleton03 s1 = Singleton03.getInstance();
        String path = "src/student/aistar/day13/singleton.txt";
        //把这个对象写入到文件中
        test01(s1, path);
        Singleton03 s2 = test02("src/student/aistar/day13/singleton.txt");//反序列化之后, 并没有走构造, 直接操作的内存
        System.out.println(s1 == s2);//false

    }

    //对象的序列化
    public static void test01(Singleton03 s1, String path) throws Exception {
        //写
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
        out.writeObject(s1);
        out.close();
    }

    //对象的反序列化
    public static Singleton03 test02(String path) throws Exception {
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
        //读
        Singleton03 s2 = (Singleton03) in.readObject();
        in.close();
        return s2;
    }
}
